package prog.model;


import prog.Main;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author MrKhoai
 */
public class Project {

    public static final int NONE = 0;
    public static final int RUN = 1;
    public static final int PAUSE = 2;
    public static final int STOP = 3;
    public static final int FINISHED = 4;

    private String projectName = null;
    private String URL = null;
    private String saveTo = null;
    private String partern = "";
    private int lvLimit = 0;
    private int insOp = 0;
    private int downOp = 0;

    private int status;
    public Thread[] thread;
    public ArrayList<Data> stack;

    public Project() {
        initThread();
    }

    public void initThread() {
        thread = new Thread[Main.program.getNumThread()];
        stack = new ArrayList<Data>();
    }

    public void work() throws IOException, InterruptedException {
        HtmlPage page = null;
        try {
            page = new HtmlPage(URL);
            page.setLvLimit(lvLimit);
            page.putToProject(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(page != null) {
            page.download(saveTo);
            for(int i = 0; i < thread.length; i++) {
                thread[i] = new ThreadWorker(this, i).createThread();
                thread[i].start();
            }
        }

        while(true) {
            int count = 0;
            for(Thread t : thread) {
                if(t.isAlive())
                    count++;
            }
            if(count == 0) {
                if(status == FINISHED)
                    Main.program.mainForm.pnToolbar.updateToolBar();
                writeDescription();
                Main.program.saveInstance();
                break;
            }
            else
                Thread.sleep(1000);
        }
    }

    public synchronized void putToStack(Data oj) {
        if(oj == null)
            return;
        stack.add(oj);
    }

    public synchronized void putToStack(ArrayList<Data> ojs) {
        if(ojs == null)
            return;
        for(Data oj : ojs)
            stack.add(oj);
    }

    public synchronized Data popFromStack() {
        Data data = null;
        if(stack.size() > 0) {
            data = stack.get(0);
            stack.remove(0);
        }
        return data;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public void writeDescription() {
        try {
            if(status == FINISHED) {
                File f = new File(saveTo + "Desc.a");
                BufferedWriter writer = new BufferedWriter(new FileWriter(f));
                writer.write("finish");
                writer.close();
            }
            if(status == PAUSE || status == STOP) {
                File f = new File(saveTo + "Desc.a");
                BufferedWriter writer = new BufferedWriter(new FileWriter(f));
                for(Data data:stack) {
                    writer.write(data.toString());
                    writer.newLine();
                }
                writer.close();
            }
        } catch (Exception e) {
        }
    }

// <editor-fold defaultstate="collapsed" desc="property">
    public void setName(String name) {
        projectName = name;
    }
    public String getName() {
        return projectName;
    }

    public void setURL(String url) {
        URL = url;
    }
    public String getURL() {
        return URL;
    }

    public void setDst(String dst) {
        saveTo = dst;
    }
    public String getDst() {
        return saveTo;
    }

    public void setPartern(String partern) {
        this.partern = partern;
    }
    public String getPartern() {
        return partern;
    }

    public void setLv(int lv) {
        lvLimit = lv;
    }
    public int getLv() {
        return lvLimit;
    }

    public void setInsOp(int op) {
        insOp = op;
    }
    public int getInsOp() {
        return insOp;
    }

    public void setdownOp(int op) {
        downOp = op;
    }
    public int getdownOp() {
        return downOp;
    }
// </editor-fold>
}
